package com.eascs.dubbo.api.internal;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.internal.ManageSituationService;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.ManagementInfoEntity;
import com.eascs.crm.nsEditDetail.entity.O2oMainPsInfoEntity;
import com.eascs.crm.nsEditDetail.service.IMainPsInfoService;
import com.eascs.crm.nsEditDetail.service.ManagementInfoService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Service
public class ManageSituationServiceImpl implements ManageSituationService{

	@Autowired
	private ManagementInfoService managementInfoService;
	@Reference
	IMainPsInfoService mainPsInfoService;
	
	@Override
	public String updateManageInfo(ManagementInfoEntity form) {
		User loginUser = UserHolder.getUser();
		ManagementInfoEntity entity = managementInfoService.getByLoanNo(form.getLoanapplyNo());
		if (entity == null) {
			form.setCreateId(loginUser.getNo());
			form.setCreator(loginUser.getCname());
			form.setCreateDate(new Date());
			form.setModifyId(loginUser.getNo());
			form.setModifier(loginUser.getCname());
			form.setModifyDate(new Date());
			ManagementInfoEntity saveResult = managementInfoService.save(form);
			if (saveResult == null) {
				return "修改失败";
			}
		} else {
				form.setCreateId(entity.getCreateId());
				form.setCreator(entity.getCreator());
				form.setCreateDate(entity.getCreateDate());
				form.setModifyId(loginUser.getNo());
				form.setModifier(loginUser.getCname());
				form.setModifyDate(new Date());
			int effectRow = managementInfoService.update(form);
			if (effectRow <= 0) {
				return "修改失败";
			}
		}
		return null;
	}

	@Override
	public String updatePsInfo(O2oMainPsInfoEntity form) {
		O2oMainPsInfoEntity entity = mainPsInfoService.getByLoanNo(form.getLoanapplyNo());
		if (entity == null) {
			createO2oMainShopInfo(form);
			boolean flag = mainPsInfoService.insert(form);// 新增
			if (!flag) {
				return "添加暗访内容信息失败";
			}
		} else {
			if (StringUtil.isBlank(form.getId())) {
				return "更新暗访内容信息失败:记录不存在";
			}
			O2oMainPsInfoEntity info = mainPsInfoService.getEntityById(form.getId());
			form = modifyO2oMainShopInfo(info, form);
			boolean flag = mainPsInfoService.update(form);// 修改
			if (!flag) {
				return "修改暗访内容信息失败";
			}
		}
		return null;
	}
	
	private void createO2oMainShopInfo(O2oMainPsInfoEntity info){
		User user = UserHolder.getUser();
		info.setCreateDate(new Date());
		info.setModifyDate(new Date());
		info.setCreateId(user.getNo());
		info.setModifyId(user.getNo());
		info.setCreator(user.getCname());
		info.setModifier(user.getCname());
	}
	
	private O2oMainPsInfoEntity modifyO2oMainShopInfo(O2oMainPsInfoEntity info,O2oMainPsInfoEntity form){
		User user = UserHolder.getUser();
		form.setModifyDate(new Date());
		form.setModifyId(user.getNo());
		form.setModifier(user.getCname());
		
		form.setCreateId(info.getCreateId());
		form.setCreateDate(info.getCreateDate());
		form.setCreator(info.getCreator());
		return form;
	}
}
